import axios from 'axios';
import { Message } from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

axios.defaults.timeout = 60000;


//配置地址现在打包发布不需要修改路径,如需修改去配置文件:config/dev.env.js    config/prod.env.js
axios.defaults.baseURL = process.env.API_ROOT;
//http request 拦截器
axios.interceptors.request.use(
    config => {

        var token = locache.get('token'); //注意使用的时候需要引入cookie方法，推荐js-cookie
        config.data = JSON.stringify(config.data);
        config.headers = {
            "token": token,
            "Company-No":20004,
            'Content-Type': 'application/json;charset=UTF-8',
            //"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
            //'Access-Control-Allow-Origin': '*',
            //'Access-Control-Allow-Headers': "Access-Control-Allow-Origin",
            // 'Access-Control-Allow-Headers':'Access-Control-Allow-Origin',
            // 'Access-Control-Allow-Credentials': true
        }
        return config;
    },
    error => {
        return Promise.reject(err);
    }
);


//http response 拦截器
axios.interceptors.response.use(
    response => {
        if (response.data.code == -1004) {
            Message.error({ message: '登录超时,2秒后跳转登录页面,请重新登录', center: true });
            setTimeout(function() {
                parent.location.href = '/';
            }, 2000)
        }
        return response;
    },
    error => {
        return Promise.reject(error)
    }
)

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(model) {
    var that = this;
    return new Promise((resolve, reject) => {
        if (model.load == true) {
            //   loading();
        }
        axios.post(model.url, model.data)
            .then(response => {
                if (model.load == true) {
                    //  closeLoad();
                }

                if (typeof model.success == 'function') {
                    model.success(response.data);
                }
            }, err => {
                if (model.load == true) {
                    //  closeLoad();
                }
                if (typeof model.error == 'function') {
                    this.$router.push({ path: '/error', });
                }
            })
    })
}
//小黑窗
export function toast(msg = "提示") {

}

//加载中
function loading(msg = '加载中...') {

}

//关闭加载
function closeLoad() {

}
